package Bacterias;

import java.util.List;

import Enums.AnimationType;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.SoundManager;
import com.game.ThauanFramework.Sprite;
import com.game.ThauanFramework.SpriteAnimationData;
import com.thauanLopes.bacterium.GameSceneView;
import com.thauanLopes.bacterium.GameManages.ScoreManager;
import com.thauanLopes.smagasmaga.R;

public class Bacteria extends GameObject {

	private final String TAG = "Bacteria NORMAL";

	private List<GameObject> cemetery;
	private List<GameObject> gameObjects;

	private int ScoreValue = 1;

	private int hitsToDie = 1;
	private int hitCounter = 0;

	private boolean alive = true;
	private boolean eyeState = false;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	private double cronometro;

	private Sprite sprite;
	private SpriteAnimationData sad_eyes;
	private Rect destRect;
	private int x;
	private int y;

	private GameSceneView gameSceneView;

	private Bitmap bitmap;

	public Bacteria(List<GameObject> gameObjects, List<GameObject> cemetery,
			GameSceneView gameSceneView) {

		this.cemetery = cemetery;
		this.gameObjects = gameObjects;
		this.gameSceneView = gameSceneView;

		this.sad_eyes = new SpriteAnimationData(BitmapStorage.getInstance()
				.getEye_sprite(), 7, 1, AnimationType.ONCE);

		this.bitmap = BitmapStorage.getInstance().getBacteria_comum_1();
		this.sprite = new Sprite();
		this.sprite.Start(new SpriteAnimationData(this.bitmap, 4, 2,
				AnimationType.ONCE));

		int tmpX = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasWidth() - this.sprite.getSpriteWidth()
						- 2 * (MinhasCoisas.getCanvasWidth() / 10))
				+ (MinhasCoisas.getCanvasWidth() / 10);
		int tmpY = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasHeight() - this.sprite.getSpriteHeight()
						- 2 * (MinhasCoisas.getCanvasHeight() / 10))
				+ MinhasCoisas.getCanvasHeight() / 10;

		this.destRect = new Rect(tmpX, tmpY, tmpX
				+ this.sprite.getSpriteWidth(), tmpY
				+ this.sprite.getSpriteHeight());

		boolean deu = false;
		for (int i = 0; i < 3; i++) {

			boolean da = true;
			for (GameObject gameObject : gameObjects) {
				if (gameObject.getClass().equals(Bacteria.class)) {
					Bacteria gos = (Bacteria) gameObject;
					if (this.destRect.intersect(gos.destRect)) {
						da = false;
						break;
					}
				}
			}
			if (da) {
				deu = da;
				break;
			}
		}

		if (!deu) {
			alive = false;
		} else {

			setPosition(tmpX, tmpY);

			gameObjects.add(this);
		}
	}

	@Override
	public void Update(double deltaTime) {
		if (alive) {

			this.sprite.Update(deltaTime);

			if (!this.sprite.isFinished()) {

			} else if (this.eyeState) {
				Die();
			} else {
				this.gameSceneView.setLost(true);
			}
		}
	}

	@Override
	public void Draw(Canvas canvas) {
		if (alive) {
			this.sprite.Draw(canvas, this.destRect);
		}
	}

	@Override
	public void Die() {
		this.cemetery.add(this);
		this.alive = false;
		SoundManager.getInstance().playSound(R.raw.pop_2);
		ScoreManager.getInstance().AddScore(this.ScoreValue);

	}

	@Override
	public void Destroy() {
		gameObjects.remove(this);
	}

	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
		this.destRect.set(x, y, x + this.sprite.getSpriteWidth(), y
				+ this.sprite.getSpriteHeight());
	}

	@Override
	public boolean onTouch(MotionEvent event) {

		float fingerX = event.getRawX();
		float fingerY = event.getRawY();

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			if (this.destRect.contains((int) fingerX, (int) fingerY)) {
				this.hitCounter++;
				if (this.hitCounter >= this.hitsToDie) {
					this.sprite.ChangeSprite(this.sad_eyes);
					this.eyeState = true;
				}
				return true;
			}
			break;

		}
		return false;
	}

}
